/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_client.screens;

import battleship_client.Controller;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author jalejandrovc
 */
public class LoginScreen extends Screen {

    /**
     * Creates an instance of loginScreen
     * @param c the controller of this LoginScreen
     */
    public LoginScreen(Controller c) {
        windowHandler = c;
        setLookAndFeel();
        initComponents();
    }

    /**
     * Inicialize components of the LoginScreen
     */
    private void initComponents() {
        setTitle("Welcome to Battleships");
        setSize(400, 390);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        contentPanel = getContentPane();
        mainPanel = new JPanel();
        mainPanel.setLayout(null);

        lblImage = new JLabel();
        lblWelcome = new JLabel("Welcome to Battleships!");
        lblUser = new JLabel("Username:");
        lblPassword = new JLabel("Password:");
        lblUsernameError = new JLabel("");
        lblPasswordError = new JLabel("");
        lblError = new JLabel("Error when connecting to server.");
        txtPassword = new JPasswordField("");
        txtUser = new JTextField("");
        btnLogin = new JButton("Login");
        btnNewUser = new JButton("New User");
        chkIsAdmin = new JCheckBox("Admin");
        chkIsAdmin.setVisible(true);

        lblImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/400x100.png")));
        lblImage.setBounds(0,0,400,100);
        lblWelcome.setBounds(80, 120, 300, 25);
        lblWelcome.setFont(new Font("Lucida Grande", 1, 20));
        lblUsernameError.setBounds(100, 210, 250, 20);
        lblUsernameError.setFont(new Font("Lucida Grande", 1, 11));
        lblUsernameError.setForeground(Color.red);
        lblUsernameError.setVisible(false);
        lblPasswordError.setBounds(100, 260, 250, 20);
        lblPasswordError.setFont(new Font("Lucida Grande", 1, 11));
        lblPasswordError.setForeground(Color.red);
        lblPasswordError.setVisible(false);
        lblError.setFont(new Font("Lucida Grande", 1, 11));
        lblError.setBounds(3, 330, 400, 20);
        lblError.setForeground(Color.red);
        lblError.setVisible(false);
        lblUser.setBounds(100, 180, 80, 20);
        lblPassword.setBounds(100, 230, 80, 20);
        txtUser.setBounds(200, 180, 100, 30);
        chkIsAdmin.setBounds(310, 185, 100, 20);
        txtPassword.setBounds(200, 230, 100, 30);
        btnLogin.setBounds(220, 290, 80, 30);
        final LoginScreen login = this;
        btnLogin.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblUsernameError.setVisible(false);
                lblPasswordError.setVisible(false);
                lblError.setVisible(false);
                windowHandler.validator.validateLogin(txtUser.getText(), txtPassword.getPassword(), chkIsAdmin.isSelected(), login);
            }
        });
        btnNewUser.setBounds(100, 290, 100, 30);
        btnNewUser.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                windowHandler.newUser();
            }
        });

        mainPanel.add(lblImage);
        mainPanel.add(lblWelcome);
        mainPanel.add(lblUser);
        mainPanel.add(lblPassword);
        mainPanel.add(lblUsernameError);
        mainPanel.add(lblPasswordError);
        mainPanel.add(lblError);
        mainPanel.add(txtPassword);
        mainPanel.add(txtUser);
        mainPanel.add(btnLogin);
        mainPanel.add(btnNewUser);
        mainPanel.add(chkIsAdmin);
        contentPanel.add(mainPanel);
    }

    public void setLblUsernameErrorText(String message) {
        lblUsernameError.setText(message);
        lblUsernameError.setVisible(true);
    }

    public void setLblPasswordErrorText(String message) {
        lblPasswordError.setText(message);
        lblPasswordError.setVisible(true);
    }

    public void setLblErrorText(String message) {
        lblError.setText(message);
        lblError.setVisible(true);
    }
    private Controller windowHandler;
    private Container contentPanel;
    private JPanel mainPanel;
    private JLabel lblImage;
    private JLabel lblUser;
    private JLabel lblWelcome;
    private JLabel lblPassword;
    private JLabel lblUsernameError;
    private JLabel lblPasswordError;
    private JLabel lblError;
    private JTextField txtUser;
    private JPasswordField txtPassword;
    private JButton btnLogin;
    private JButton btnNewUser;
    private JCheckBox chkIsAdmin;
}
